"use strict";
/* Controllers */
app.controller("agentAddController", ['$scope', '$q', '$location', '$rootScope', '$routeParams', 'agentAddService', function ($scope, $q, $location, $rootScope, $routeParams, agentAddService) {

    var vm = $scope.vm = {};
    vm.p = {};
    vm.p.type = $routeParams.type; // 如果id!=0为更新
    if(vm.p.type=='rg') {
//        vm.p.resolvegroupId = $routeParams.id;
        vm.p.id = 0;
    }else if (vm.p.type=='agent_id') {
        vm.p.agentId = $routeParams.id;
    }else {
        vm.p.id = $routeParams.id;
    }

    var errorFn = function (result) {
        alert(result);
    };

//    $scope.addResolvegroup = function () {
//        var vm = $scope.vm;
//        var params = vm.p;
//        var procFn = function (response) {
//            if (response.success && response.data) {
//                alert("执行成功！")
//            } else {
//                alert(response.msg);
//            }
//        };
//        return agentAddService.addResolvegroup(params, procFn, errorFn);
//    };

    $scope.addAgent = function () {
        var vm = $scope.vm;
        var params = vm.p;
        var procFn = function (response) {
            if (response.success && response.data) {
                alert("执行成功！")
            } else {
                alert(response.msg);
            }
        };
        return agentAddService.addAgent(params, procFn, errorFn);
    };

    $scope.getAgent = function () {
        var vm = $scope.vm;
        var params = {
            id: vm.p.id,
            agentId:vm.p.agentId
        };
        var procFn = function (res) {
            if (res.success && res.data) {
                vm.p = res.data;
            } else {
                alert(res.msg);
            }
        };
        return agentAddService.getAgent(params, procFn, errorFn);
    };
    if (vm.p.id != 0) {
        console.log(vm.p.id);
        vm.isDisabled = true
        $scope.getAgent();
    } else {
        vm.isDisabled = false
    }
}]);


/* Services */
app.service('agentAddService', ['ajaxService', function (ajaxService) {

//    this.listResolveGroup = function (params, successFunction, errorFunction) {
//        return ajaxService.AjaxPost(params, "/star/api/onduty-app-service/v1/resolvegroup/listResolvegroup.do", successFunction, errorFunction);
//    };

//    this.listCti = function (params, successFunction, errorFunction) {
//        ajaxService.AjaxPost(params, "/star/api/onduty-app-service/v1/cti/listCti.do", successFunction, errorFunction);
//    };


    this.getAgent = function (params, successFunction, errorFunction) {
        return ajaxService.AjaxPost(params, "/star/api/onduty-app-service/v1/api_report/getApiReport.do", successFunction, errorFunction);
    };

    this.addAgent = function (params, successFunction, errorFunction) {
        return ajaxService.AjaxPost(params, "/star/api/onduty-app-service/v1/api_report/addApiReport.do", successFunction, errorFunction);
    };

}]);

